package com.mingjie.daydayup;

import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Xml;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;

import domain.News;
import domain.SmartImage;
import domain.SmartImageView;
import util.utils;

public class NewsActivity extends AppCompatActivity {

    private ArrayList<News> newslist;
    private ListView list;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    list.setAdapter(new MyAdapter());
            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news);
        list = (ListView) findViewById(R.id.lv_news);
        System.out.println(111);
        getNews();
    }

    public void getNews() {
        Thread t = new Thread() {
            @Override
            public void run() {
                String path = "http://192.168.224.2:8080/news.xml";

                try {
                    URL url = new URL(path);
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    conn.setRequestMethod("GET");
                    conn.setConnectTimeout(5000);
                    conn.setReadTimeout(5000);
                    //发送http GET请求，获取相应码
                    if (conn.getResponseCode() == 200) {
                        System.out.println(222);
                        InputStream is = conn.getInputStream();
                        //使用pull解析器，解析这个流
//                        System.out.println(utils.Stream2String(is,"utf-8"));
                        parseNewsXml(is);
                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        };
        t.start();
    }

    class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return newslist.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            News news = newslist.get(position);
            View v = null;
            ViewHolder mHolder = null;
            if (convertView == null) {
                v = View.inflate(NewsActivity.this, R.layout.item_listview, null);
                mHolder = new ViewHolder();
                mHolder.tv_title = (TextView) v.findViewById(R.id.tv_title);
                mHolder.tv_detail = (TextView) v.findViewById(R.id.tv_detail);
                mHolder.tv_comment = (TextView) v.findViewById(R.id.tv_comment);
                mHolder.siv = (SmartImageView) v.findViewById(R.id.iv);
                v.setTag(mHolder);
            } else {
                v = convertView;
                mHolder = (ViewHolder) v.getTag();
            }
            mHolder.tv_title.setText(news.getTitle());

            mHolder.tv_detail.setText(news.getDetail());

            mHolder.tv_comment.setText(news.getComment() + "条评论");

            //给新闻图片imageview设置内容

            mHolder.siv.setImageUrl(news.getImageUrl());
            return v;

        }

        class ViewHolder {
            TextView tv_title;
            TextView tv_detail;
            TextView tv_comment;
            SmartImageView siv;
        }
    }

    /**
     * 解析xml数据
     *
     * @param is xml输入流
     * @throws IOException
     */
    public void parseNewsXml(InputStream is) throws IOException {
        XmlPullParser parser = Xml.newPullParser();
        try {
            parser.setInput(is, "utf-8");
            int type = parser.getEventType();
            News news = null;
            while (type != XmlPullParser.END_DOCUMENT) {
                switch (type) {
                    case XmlPullParser.START_TAG:
                        if ("newslist".equals(parser.getName())) {
                            newslist = new ArrayList<>();
                        } else if ("news".equals(parser.getName())) {
                            news = new News();
                        } else if ("title".equals(parser.getName())) {
                            news.setTitle(parser.nextText());
                        } else if ("detail".equals(parser.getName())) {
                            news.setDetail(parser.nextText());
                        } else if ("comment".equals(parser.getName())) {
                            news.setComment(parser.nextText());
                        } else if ("image".equals(parser.getName())) {
                            news.setImageUrl(parser.nextText());
                        }
                        break;
                    case XmlPullParser.END_TAG:
                        if ("news".equals(parser.getName()))
                            newslist.add(news);
                        break;
                }
                type = parser.next();

            }
            for (News n : newslist) {
                System.out.println(n.toString());
            }
            Message msg = Message.obtain();
            msg.what = 1;
            handler.sendMessage(msg);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
